import { Config } from "../config/Config";
import { ErrorCode } from "../config/ErrorCode";
import DateUtil from "../engine/utils/DateUtil";
import { Logger } from "../engine/utils/Logger";
import { Md5Helper } from "../engine/utils/Md5Helper";
import { WebUtil } from "../engine/utils/WebUtil";

const routerMap = {
}

export class GameWebReqHandler {

    public static instance: GameWebReqHandler = new GameWebReqHandler();

    public init(app) {
        for (const key in routerMap) {
            app.use(routerMap[key], this.onBaseRoot.bind(this))
        }
    }

    private onBaseRoot(req, resp) {
        try {
            let ip: string = WebUtil.getIpByExpressReq(req);
            
        } catch (error) {
            Logger.warn("onBaseRoot error", error);
        }
    }

    private checkBody(ip: string, req, resp, callback: Function) {
        if (req.body) {
            try {
                let jsonObj = JSON.parse(req.body)
                let k: string = jsonObj["k"];
                let data: object = jsonObj["data"]
                if (k == Md5Helper.getMd5_encrypt(JSON.stringify(data))) {
                    callback && callback(req, resp, data, ip);
                }
                else {
                    Logger.error("收到非法Http请求 key", ip, req.body)
                    resp.end();
                }
            } catch (error) {
                Logger.error("收到非法Http请求body", ip, req.body, error)
                resp.end();
            }
        }
        else {
            resp.end();
        }
    }



}